#include "History.h"
History :: History(Database * db)
: db(db)
{
}
void History :: insertToHistory(Job * job)
{
	db->execStatement("insert into HistTab (exec,args,dir_id) values("+db->addString(job->getCMD())+","+db->addString(job->getARGS())+","+db->addString(job->getCWD())+")");
}
std::vector<std::string> History :: getHistory(HistoryOrder order)
{
	return getHistory("./",order);
}
std::vector<std::string> History :: getHistory(std::string path,HistoryOrder order)
{
	std::string stmt = "select  (select value from StrTab where uid==exec)||(select value from StrTab where uid==args) from HistTab as e1 ";
	if( (order&1) == History::TimeOrder)
	{
		stmt += "where not exists ( select id from HistTab e2 where e2.id==e1.id+1 and e1.exec||e1.args==e2.exec||e2.args) ";
	}
	else
	if( (order&1) == History::FreqOrder)
	{
		stmt += "group by exec||args order by count(exec||args) ";
	}
	if( (order&2) == History::GlobOrder)
	{
		stmt += "and dir_id=="+db->addString(path);
	}
	ValueTable * vt = db->makeQuery(stmt);
	std::vector<std::string> retv;
	if(vt)
	{
		retv = (*vt)[0].second;
		delete vt;
	}
	return retv;
}
History :: ~History()
{
}
